package de.baumann.browser.views.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import de.baumann.browser.OdinGlideModule;
import de.baumann.browser.R;
import de.baumann.browser.adapter.OperateAdapter;
import de.baumann.browser.utils.UserDataKt;


/**
 * @author：
 * @date：2018/9/18
 * @discription: 操作菜单
 **/
public class MenuDialog extends BaseDialog implements View.OnClickListener {
    private ImageView mAccountImage;
    private TextView mAcconutName;

    public MenuDialog(@NonNull Context context) {
        super(context);
    }

    private MenuClickListener listener;

    public void setOnClickListener(MenuClickListener listener) {
        this.listener = listener;
    }

    @Override
    protected int laoutId() {
        return R.layout.dialog_menu_operate;
    }

    @Override
    protected int gravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected void initView() {
        findViewById(R.id.ibMenuClose).setOnClickListener(this);
        findViewById(R.id.ibMenuSetting).setOnClickListener(this);
        mAccountImage = findViewById(R.id.img_account_img);
        mAcconutName = findViewById(R.id.tv_account_name);
        mAccountImage.setOnClickListener(this);
        mAcconutName.setOnClickListener(this);
        RecyclerView menuRecycler = findViewById(R.id.recycler_menu_operate);
        menuRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        OperateAdapter operateAdapter = new OperateAdapter();
        menuRecycler.setAdapter(operateAdapter);
        operateAdapter.setOnItemClickListener((adapterView, view, position, l) -> {
            dismiss();
            switch (position) {
                case 0:
                    listener.onClickAddBookmark();
                    break;
                case 1:
                    listener.onClickHisttory();
                    break;
                case 2:
                    listener.onClickRefresh();
                    break;
                case 3:
                    listener.onClickAbout();
                    break;
                case 4:
                    listener.onClickFeedback();
                    break;
                case 5:
                    listener.onClickWindows();
                    break;
                default:
                    dismiss();
            }
        });
    }

    @Override
    public void show() {
        loadUserInfo();
        super.show();
    }

    private void loadUserInfo() {
        if (UserDataKt.isLogined()) {
            mAcconutName.setText(UserDataKt.getUserName());
            OdinGlideModule.loadWithCropAndCircle(mContext, UserDataKt.getUserHead(), mAccountImage, R.drawable.icon_head);
            return;
        }
        mAcconutName.setText(mContext.getString(R.string.login_odin));
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_account_img:
            case R.id.tv_account_name:
                dismiss();
                listener.onClickLogin();
                break;
            case R.id.ibMenuSetting:
                dismiss();
                listener.onClickSetting();
                break;
            default:
                dismiss();
        }
    }

    public interface MenuClickListener {


        /**
         * 登录
         */
        void onClickLogin();

        /**
         * 点击添加书签
         */
        void onClickAddBookmark();

        /**
         * 点击书签和历史
         */
        void onClickHisttory();

        /**
         * 点击刷新
         */
        void onClickRefresh();

        /**
         * 点击设置
         */
        void onClickSetting();

        /**
         * 关于
         */
        void onClickAbout();

        /**
         * 意见反馈
         */
        void onClickFeedback();

        /**
         *
         */
        void onClickWindows();

        /**
         * 点击下载
         */
        void onClickDownload();

    }
}
